﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace Exact_Extension
{
    public partial class MsgSetDiscount : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        Common common;
        ISHCMC_V_StudentListWithDebtor itemStudent;
        int LastRowHandleDiscount = 0;
        public MsgSetDiscount(ExactDataContext _DS , Common _common,ISHCMC_V_StudentListWithDebtor _itemStudent)
        {
            InitializeComponent();

            DS = _DS;
            common = _common;
            itemStudent = _itemStudent;

            
        }
        private void LoadDiscountPerStudent(string studentID)
        {
            var queryDis = from Dis in DS.ISHCMC_V_DiscountLists
                           where Dis.StudentID == studentID
                           select Dis;
            this.iSHCMCVDiscountListBindingSource.DataSource = queryDis;

        }
        private void btnSave_Click(object sender, EventArgs e)
        {
             string discounttype ="";
        
            int flag = 0;
            if (this.lookUpEdit1.EditValue != null)
            {
                discounttype = this.lookUpEdit1.EditValue.ToString();
                flag = 1;
                if (!common.CheckDiscountExist(itemStudent.StudentID, discounttype))
                {


                    ISHCMC_Discount dis = new ISHCMC_Discount
                      {
                          Debcode = itemStudent.DebtorCode,
                          StudentID = itemStudent.StudentID,
                          DiscountType = discounttype,
                          syscreated = DateTime.Now,
                          sysmodified = DateTime.Now,
                          Status = 0
                      };
                    DS.ISHCMC_Discounts.InsertOnSubmit(dis);
                    DS.SubmitChanges();

                    this.LoadDiscountPerStudent(itemStudent.StudentID);
                }
                else
                {
                    MessageBox.Show("Discount type already assigned to this student , please try to check again !!");
                }



            }
            else
            {
                flag = 0;
                MessageBox.Show("Please choose discount type !!");


            }
           
        }

        private void MsgSetDiscount_Shown(object sender, EventArgs e)
        {
            var query = from DM in DS.ISHCMC_DiscountMasters
                        select DM;
            this.lookUpEdit1.Properties.DataSource = query;
            if(query.Count() >0)
            {
                this.lookUpEdit1.EditValue = query.First().DiscountName;
            }
            this.LoadDiscountPerStudent(itemStudent.StudentID);
        }

        private void gridView_Discount_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.popupMenu1.ShowPopup(Control.MousePosition);
                LastRowHandleDiscount = e.RowHandle;
            }
          
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_V_DiscountList dis = (ISHCMC_V_DiscountList)this.gridView_Discount.GetRow(LastRowHandleDiscount);
            int result = common.DeleteDiscount(dis.ID);
            if (result == 0)
            {
                MessageBox.Show("Can't delete discount , please check with administrator! ");
            }
            else
            {
                this.LoadDiscountPerStudent(itemStudent.StudentID);
            }
        }
    }
}